package com.lineage.data.item_etcitem.add;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.sql.CharItemsTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ItemStatus;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.RandomArrayList;

public class Enchant_Scroll_CHA extends ItemExecutor {
  private int type1;
  
  private int type2;
  
  public static ItemExecutor get() {
    return new Enchant_Scroll_CHA();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int targObjId = data[0];
    L1ItemInstance l1iteminstance = pc.getInventory().getItem(targObjId);
    if (l1iteminstance == null)
      return; 
    if (!pc.getInventory().checkItem(l1iteminstance.getItem().getItemId(), 1L))
      return; 
    if (l1iteminstance.isEquipped()) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("该装备尚未脱掉。"));
      return;
    } 
    if (l1iteminstance.getItemCha() == this.type1) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("该武器能力已达上限"));
      return;
    } 
    if (l1iteminstance.getItem().getType2() == 1)
      if (RandomArrayList.getInc(100, 1) <= this.type2) {
        l1iteminstance.setItemCha(l1iteminstance.getItemCha() + 1);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("强化成功"));
      } else {
        if (l1iteminstance.getItemCha() == 0) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("没发生什么事情"));
          return;
        } 
        l1iteminstance.setItemCha(l1iteminstance.getItemCha() - 1);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("强化魅力失败"));
      }  
    pc.getInventory().removeItem(item, 1L);
    pc.sendPackets((ServerBasePacket)new S_ItemStatus(l1iteminstance));
    CharItemsTable cit = new CharItemsTable();
    try {
      cit.updateItemCha(l1iteminstance);
      pc.save();
    } catch (Exception e) {
      e.printStackTrace();
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this.type1 = Integer.parseInt(set[1]);
    } catch (Exception exception) {}
    try {
      this.type2 = Integer.parseInt(set[2]);
    } catch (Exception exception) {}
  }
}
